package com.zrc.module_find.fragment

import android.content.Intent
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmFragment
import com.zrc.module_find.BR
import com.zrc.module_find.R
import com.zrc.module_find.databinding.FragmentFindBinding
import com.zrc.module_find.viewmodel.FindVM
import com.zrc.superframe_tbs.FileReaderActivity
import android.media.MediaMetadataRetriever

import android.graphics.Bitmap
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import java.lang.IllegalArgumentException


/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.FRAGMENT_FIND, name = "发现")
class FindFragment : BaseMvvmFragment<FragmentFindBinding, FindVM>() {

    var permissions = arrayListOf(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE, Permission.CAMERA)

    override fun getVariableId() = BR.viewModel

    override fun getLayoutId() = R.layout.fragment_find

    override fun getViewModel() = FindVM(this)

    override fun initObserver() {
        super.initObserver()
        mBinding.tvBtn1.setOnClickListener(this)
        mBinding.tvBtn2.setOnClickListener(this)
        mBinding.tvBtn3.setOnClickListener(this)
        mBinding.tvBtn4.setOnClickListener(this)
        mBinding.tvBtn5.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        when (view) {
            mBinding.tvBtn1 -> {
                //requestPermission(*permissions.toTypedArray(),success={
                //    mViewModel.getData()
                //})
                Log.e("zrc", view.id.toString())
            }

            mBinding.tvBtn2 -> {
                val intent = Intent(this.context, FileReaderActivity::class.java)
                intent.putExtra("path", "http://julgg.cjs.com.cn/CN_DISC/STOCK_NT/2020/04/30/012001472_ls_1207690450.pdf")
                startActivity(intent)
            }

            mBinding.tvBtn3 -> {
                ARouter.getInstance()
                    .build(ARouterHub.ACTIVITY_STATEPAGEDEMO)
                    .withBoolean("sm",true)
                    .navigation(context)
            }

            mBinding.tvBtn4 -> {
                ARouter.getInstance()
                    .build(ARouterHub.ACTIVITY_DEFRECYCLEVIEW)
                    .navigation(context)
            }

            mBinding.tvBtn5 -> {
                ToastUtils.showShort("niaho")
//                thread {
//                    mBinding.jjjjjjj.setImageBitmap(getNetVideoBitmap("https://video.pearvideo.com/mp4/adshort/20210105/cont-1715046-15562045_adpkg-ad_hd.mp4"))
//                }
            }

        }
    }

    private fun getNetVideoBitmap(videoUrl: String?): Bitmap? {
        var bitmap: Bitmap? = null
        val retriever = MediaMetadataRetriever()
        try {
            //根据url获取缩略图
            retriever.setDataSource(videoUrl, HashMap())
            //获得第一帧图片
            bitmap = retriever.frameAtTime
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } finally {
            retriever.release()
        }
        return bitmap
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            XXPermissions.REQUEST_CODE -> {
                if (isShow) {//只有显示的fragment才处理从设置界面返回的code（避免多个fragment重复处理）
                    if (XXPermissions.isGranted(activity, permissions)) {//权限全部通过了 去做自己想做的事儿吧

                    }
                }
            }
        }
    }
}